package dataStructure;

import java.net.URL;
import java.util.ArrayList;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

import android.app.Activity;
import android.util.Log;

public class DataCore extends Activity {
	//from here loop through data structure and make checks
		
	public static DataObject contentObject = null;
	public static DataLayerObject layerObject = null;
	public DataCore()
	{
		//import xml on poweron
		Log.i("XML", "Strarting import and handling");
		if(importAndHandleXML())//import and handling succeeded
		{
			//print user message?
			Log.i("XML", "Import and handling succeeded");
			
		}
		else//import and handling failed
		{
			//print user message
			Log.w("XML", "Import and handling failed");
		}
		//testObjectsFromAllLayer();
		testObjectsProporties();
	}
	
		
	public boolean importAndHandleXML()
	{
		try {
			
			//ready 
			SAXParserFactory spf = SAXParserFactory.newInstance();
			SAXParser sp = spf.newSAXParser();
			XMLReader xr = sp.getXMLReader();

			//Send URL to parse XML Tags
			URL sourceUrl = new URL("http://gis.noord-holland.nl/ArcGIS/services/PNH_structuurvisie/MapServer/WMSServer?request=GetCapabilities&service=WMS");

			// Create handler to handle XML Tags (extends DefaultHandler ) 
			HandleXML myXMLHandler = new HandleXML();

			xr.setContentHandler(myXMLHandler);			
			xr.parse(new InputSource(sourceUrl.openStream()));			
		
		} 
		catch (Exception e) {
			return false;		
		}
		return true;
	}
	
	//HELPERS
	public  ArrayList <DataLayerObject> getAllLayerObjects() //returns all level 3 Layer objects
	{
		 ArrayList <DataLayerObject> tempList = new  ArrayList <DataLayerObject>();
		for(int i = 0; i <layerObject.getChildren().size(); i++)
		{					
			for(int j=0;j<((DataLayerObject) layerObject.getChildren().get(i)).getChildren().size();j++)
			{
				tempList.add(((DataLayerObject) layerObject.getChildren().get(i).getChildren().get(j)));
			}
		}
		return tempList;
	}
	
	public  ArrayList <DataLayerObject> getVisibleLayerObjects() //returns all visible level 3 Layer objects
	{
		 ArrayList <DataLayerObject> tempList = new  ArrayList <DataLayerObject>();
		for(int i = 0; i <layerObject.getChildren().size(); i++)
		{			
			if(((DataLayerObject) layerObject.getChildren().get(i)).isVisible())
			{			
				for(int j=0;j<((DataLayerObject) layerObject.getChildren().get(i)).getChildren().size();j++)
				{
					tempList.add(((DataLayerObject) layerObject.getChildren().get(i).getChildren().get(j)));
				}
			}
		}
		return tempList;
	}
	
	public  ArrayList <DataLayerObject> getCats() //returns all level 2 Layer objects
	{
		
		ArrayList <DataLayerObject> tempList = new  ArrayList <DataLayerObject>();
		for(int i = 0; i <layerObject.getChildren().size(); i++)
		{			
			tempList.add((DataLayerObject) layerObject.getChildren().get(i));
		}
		return tempList;
	}
	
	
	public void testObjectsFromAllLayer()
	{
		for(int i = 0;i<getAllLayerObjects().size();i++)
		{
			DataLayerObject tempObject = getAllLayerObjects().get(i);
			
			Log.i("TEST", "----------"+i+"-----------");
			tempObject.testProperties();
			Log.i("TEST", "------end-"+i+"-----------");
		}
	}
	
	public void testObjectsProporties()
	{
		
		layerObject.testProperties();		
		//Log.i("TEST nr childs", ""+layerObject.getChildren().size());
		for(int i = 0; i <layerObject.getChildren().size(); i++)
		{
			Log.i("TEST", "----------"+i+"-----------");
			((DataLayerObject) layerObject.getChildren().get(i)).testProperties();
			Log.i("TEST", "---------childs----------");
			((DataLayerObject) layerObject.getChildren().get(i)).testChildTagNames();
			Log.i("TEST", "------end-"+i+"-----------");
		}
	}
	
	//END HELPERS
	
}
